async function createMap(options) {
  return await new Promise((resolve) => {
    const div = document.createElement('div');
    div.id = 'map-' + (Number(String(Math.random()).slice(2)) + Date.now()).toString(36);
    div.style.cssText = 'position: absolute;z-index: 0;top: 0;right: 0;bottom: 0;left: 0;margin: 0;visibility: hidden;';
    document.body.appendChild(div);
    const opt = {
      // mapStyle: 'amap://styles/cfd800a417c5e6683065b909ce2cee62',
      features: ['bg', 'point', 'road'],
    };
    if (typeof options === 'function') {
      options = options(opt);
    }
    options = Object.assign({}, opt, options);
    const map = new AMap.Map(div, options);
    map.loaded ? resolve(map) : map.on('complete', () => resolve(map));
  });
}
export default {
  async getMap(options) {
    return await createMap(options);
  },
  async getSingleMap(options) {
    if (!window.__SINGLE_INSTANCE_MAP) {
      window.__SINGLE_INSTANCE_MAP = await createMap(options);
      window.__SINGLE_INSTANCE_MAP.destroy = function () {
        let container = window.__SINGLE_INSTANCE_MAP.getContainer();
        if (container && container.parentNode) {
          container.parentNode.removeChild(container);
        }
        window.__SINGLE_INSTANCE_MAP.clearLimitBounds();
        window.__SINGLE_INSTANCE_MAP.clearInfoWindow();
        window.__SINGLE_INSTANCE_MAP.clearMap();
      };
    }
    return window.__SINGLE_INSTANCE_MAP;
  },
};
